import logging
import multiprocessing

from langchain.document_loaders import TextLoader
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter,CharacterTextSplitter
from langchain.vectorstores import FAISS

# 设置日志记录
logging.basicConfig(level=logging.INFO)
# 创建一个全局锁
docsearch_lock = multiprocessing.Lock()
embedding_path = '/home/linweibin/liujian/model/zpoint_large_embedding_zh'
embedding_path = '/home/linweibin/tender/bge-large-zh-v1.5'

from openai import OpenAI


def chat(prompt):
    client = OpenAI(
        base_url="http://192.168.80.35:8000/v1",
        api_key="token-abc123",
    )
    completion = client.chat.completions.create(
        model="/home/zhengzhenzhuang/liujian/model/Qwen2.5-72B-Instruct-GPTQ-Int8",
        messages=[
            {"role": "user", "content": prompt}
        ],
        temperature=0,
    )
    return completion.choices[0].message.content


def qa(filePath, question,question2):
    loader = TextLoader(filePath)
    documents = loader.load()
    # 分割文本
    # text_splitter = RecursiveCharacterTextSplitter(chunk_size=0, chunk_overlap=0, separators=["#####"])
    #text_splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=40)

    # 创建分割器
    text_splitter = CharacterTextSplitter(
        separator="\n",  # 设置单换行符作为分隔符
        chunk_size=800,  # 设置块的大小为1000个字符
        chunk_overlap=100,  # 设置重叠字符为150个字符
        length_function=len  # 长度函数设置为python的len函数
    )
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
    concatenated_content=""
    texts = text_splitter.split_documents(documents)
    k=0
    for text in texts:
        k = k + 1
        page_content = text.page_content
        n = len(page_content)
        concatenated_content += page_content + f"\n==================123132====================================================================={k}-{n}\n\n\n"
        # print("concatenated_content", concatenated_content)
        # 如果需要在每个片段之间添加分隔符，可以取消下面一行的注释
        # concatenated_content += "\n---\n"  # 或者使用其他分隔符

    wf = open("/home/linweibin/liujian/project/policy-tender/test1.txt", "w", encoding="UTF-8")
    wf.write(concatenated_content)
    wf.flush()
    wf.close()




    # 创建嵌入
    embeddings = HuggingFaceEmbeddings(model_name=embedding_path)
    # 使用锁来确保进程安全

    docsearch = FAISS.from_documents(texts, embeddings)
    results = docsearch.similarity_search(question, k=50)
    # embedding_vector = embeddings.embed_query(question)
    # results = docsearch.similarity_search_by_vector(embedding_vector, k=30)

    #results = docsearch.similarity_search_with_score(question,k=50)
    # print(results[0])
    # print(type(results[0]))
    # print(results[0][0].page_content)
    # print(results[0][1])

    # docsearch = Chroma.from_documents(texts, embeddings)
    # results = docsearch.similarity_search(question, k=30)
    # results = docsearch.as_retriever(search_type="mmr", search_kwargs={'k': 30})

    # print("results",results)
    concatenated_content = ""
    # 遍历结果，并将page_content拼接起来
    k = 0;
    for result in results:
        k = k + 1
        page_content = result.page_content
        n = len(result.page_content)
        concatenated_content += page_content + f"\n==================123132==============================={k}-{n}\n\n\n"
        # print("concatenated_content", concatenated_content)
        # 如果需要在每个片段之间添加分隔符，可以取消下面一行的注释
        # concatenated_content += "\n---\n"  # 或者使用其他分隔符

    wf = open("/home/linweibin/liujian/project/policy-tender/test2.txt", "w", encoding="UTF-8")
    wf.write(concatenated_content)
    wf.flush()
    wf.close()
    print(f"最终长度：{len(concatenated_content)}")
    prompt_template = (
        """我给你知识文本片段，片段用#####分隔开，还有一个相关的问题,请你根据片段中的原文回答我的问题,不能够省略，不能总结。如果遇到表格的内容，按照每一点原文罗列出来。如果你无法在知识文本片段中搜寻到问题的答案,只需要告诉我知识文本片段中无相关信息.\n问题：{question}\n知识文本片段：\n{context}        """)
    prompt_text = prompt_template.format(context=concatenated_content, question=question2)



path = "/home/linweibin/liujian/project/policy-tender/data/办公家具new/常规问题/办公家具招标all.txt"
qa(path, "赤峰市政府采购中心受内蒙古自治区赤峰监狱委托，采用公开招标方式组织采购办公家具及设备购置项目", "预算金额是多少元")

